package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.adapter.QuestionAnswerAdapter;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.AnswersEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.JudgeStatus;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.PhotosEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Question;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Status;
import com.neusoft.zqygbc.neusoft_wenda.model.CollectModel;
import com.neusoft.zqygbc.neusoft_wenda.model.FollowModel;
import com.neusoft.zqygbc.neusoft_wenda.model.QusetionModel;
import com.neusoft.zqygbc.neusoft_wenda.model.UserModel;
import com.neusoft.zqygbc.neusoft_wenda.view.NineGridTestLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class QuestionContentActivity extends BaseAvtivity {

    private Intent intent;
    private SharedPreferences sp;
    private ImageView question_detail_return,detail_collect_img,detail_thumbup_img,detail_userphoto;
    private TextView question_detail_username, question_detail_createtime, detail_description,
            detail_follow,detail_collect,detail_point_number,detail_thumbup;
    private LinearLayout ll_detail_collect,ll_detail_answer,ll_detail_thumbup,detail_userinfo;
    private NineGridTestLayout detail_nine_grid;
    private QusetionModel questionModel;
    private FollowModel followModel;
    private CollectModel collectModel;
    private UserModel userModel;
    private Call call;
    private RetrofitCallback<Question> questionCallback,answerCallback;
    private RetrofitCallback<JudgeStatus> judgeStatusCallback;
    private RetrofitCallback<Status> followCallback,delFollowCallback,statusCallback;
    private String followstatus,collectstatus;
    private String uid;
    private RecyclerView detail_answer_list;

    @Override
    void initViews() {
        setLayout(R.layout.activity_question_content);
        intent = getIntent();
        uid=intent.getStringExtra("question_uid");
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        question_detail_return = (ImageView) findViewById(R.id.detail_return);
        question_detail_username = (TextView) findViewById(R.id.detail_username);
        question_detail_createtime = (TextView) findViewById(R.id.detail_createtime);
        detail_description = (TextView) findViewById(R.id.detail_description);
        detail_follow = (TextView) findViewById(R.id.detail_follow);
        ll_detail_collect = (LinearLayout) findViewById(R.id.ll_detail_collect);
        ll_detail_answer = (LinearLayout) findViewById(R.id.ll_detail_answer);
        ll_detail_thumbup = (LinearLayout) findViewById(R.id.ll_detail_thumbup);
        detail_collect = (TextView) findViewById(R.id.detail_collect);
        detail_thumbup = (TextView) findViewById(R.id.detail_thumbup);
        detail_collect_img = (ImageView) findViewById(R.id.detail_collect_img);
        detail_thumbup_img = (ImageView) findViewById(R.id.detail_thumbup_img);
        detail_nine_grid = (NineGridTestLayout) findViewById(R.id.detail_nine_grid);
        detail_point_number = (TextView) findViewById(R.id.detail_point_number);
        detail_userphoto=(ImageView) findViewById(R.id.detail_userphoto);
        detail_userinfo=(LinearLayout) findViewById(R.id.detail_userinfo);
        detail_answer_list=(RecyclerView) findViewById(R.id.detail_answer_list);
        detail_answer_list.setLayoutManager(new LinearLayoutManager(QuestionContentActivity.this));
        isFollow();
        isCollect();
    }


    @Override
    void initEvents() {
        detail_userphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(QuestionContentActivity.this,UserInfoActivity.class);
                intent.putExtra("question_uid",uid);
                startActivity(intent);

            }
        });
        detail_userinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(QuestionContentActivity.this,UserInfoActivity.class);
                intent.putExtra("question_uid",uid);
                startActivity(intent);

            }
        });

        question_detail_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        detail_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (followstatus.equals("0")) {
                    follow();
                } else {
                    delFollow();
                }
            }
        });
        ll_detail_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });
        ll_detail_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(QuestionContentActivity.this, AnswerActivity.class);
                intent1.putExtra("qid", intent.getStringExtra("qid"));
                startActivity(intent1);
            }
        });
        ll_detail_thumbup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ThumbUp();
            }
        });

    }

    @Override
    void initData() {
        questionModel = new QusetionModel();
        call = questionModel.getQuestionDetail(intent.getStringExtra("qid"));
        questionCallback = new RetrofitCallback<Question>() {
            @Override
            public void onResponse(Question question) {
                QuestionEntity questionEntity = question.getQuestionDetail().getQuestion();
                question_detail_username.setText(questionEntity.getUsername());
                question_detail_createtime.setText(questionEntity.getCreatetime());
                detail_description.setText(questionEntity.getDescription());
                detail_point_number.setText(questionEntity.getThumbup());
                List<PhotosEntity> photosEntity = questionEntity.getPhotos();
                List<String> list = new ArrayList<String>();
                for (int i = 0; i < photosEntity.size(); i++)
                    list.add("http://115.159.93.244/wenda/Public/questionimg/" + photosEntity.get(i).getFilename());
                detail_nine_grid.setIsShowAll(false); //当传入的图片数超过9张时，是否全部显示
                detail_nine_grid.setSpacing(16); //动态设置图片之间的间隔
                detail_nine_grid.setUrlList(list);

            }
        };

        questionModel.bindCallback(call, questionCallback);
        questionModel = new QusetionModel();
        call = questionModel.getQuestionDetail(intent.getStringExtra("qid"));
        answerCallback = new RetrofitCallback<Question>() {
            @Override
            public void onResponse(Question question) {
                List<AnswersEntity> answersEntityList =question.getQuestionDetail().getAnswers();
                detail_answer_list.setAdapter(new QuestionAnswerAdapter(QuestionContentActivity.this, answersEntityList, R.layout.cardview_detail_answer));

            }
        };
        questionModel.bindCallback(call, answerCallback);

    }


    private void isFollow() {
        followModel = new FollowModel();
        call = followModel.isfollow(getUser_id(), uid);
        judgeStatusCallback = new RetrofitCallback<JudgeStatus>() {
            @Override
            public void onResponse(JudgeStatus judgeStatus) {
                followstatus = judgeStatus.getCode();
                if (followstatus.equals("0")) {
                    detail_follow.setText("+未关注");
                    detail_follow.setTextColor(getResources().getColor(R.color.buttontextcolor));
                } else {
                    detail_follow.setText("√已关注");
                    detail_follow.setTextColor(getResources().getColor(R.color.textcolor));
                }
            }
        };
        followModel.bindCallback(call, judgeStatusCallback);
    }

    private void follow() {
        followModel = new FollowModel();
        call = followModel.follow(getUser_id(), uid);
        followCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(QuestionContentActivity.this, "已关注", Toast.LENGTH_SHORT).show();
                    detail_follow.setText("√已关注");
                    detail_follow.setTextColor(getResources().getColor(R.color.textcolor));
                    followstatus = "1";
                } else {
                    Toast.makeText(QuestionContentActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        followModel.bindCallback(call, followCallback);
    }

    private void delFollow() {
        followModel = new FollowModel();
        call = followModel.delfollow(getUser_id(), uid);
        delFollowCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(QuestionContentActivity.this, "关注取消", Toast.LENGTH_SHORT).show();
                    detail_follow.setText("+未关注");
                    detail_follow.setTextColor(getResources().getColor(R.color.buttontextcolor));
                    followstatus = "0";
                } else {
                    Toast.makeText(QuestionContentActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        followModel.bindCallback(call, delFollowCallback);
    }

    private void isCollect() {
        collectModel = new CollectModel();
        call = collectModel.iscollect(getUser_id(), intent.getStringExtra("qid"));
        judgeStatusCallback = new RetrofitCallback<JudgeStatus>() {
            @Override
            public void onResponse(JudgeStatus judgeStatus) {
                collectstatus = judgeStatus.getCode();
                if (collectstatus.equals("0")) {
                    detail_collect_img.setImageResource(R.drawable.ic_collect_normal);
                    detail_collect.setText("未收藏");
                    detail_collect.setTextColor(getResources().getColor(R.color.textcolor));
                } else {
                    detail_collect_img.setImageResource(R.drawable.ic_collect_pressed);
                    detail_collect.setText("已收藏");
                    detail_collect.setTextColor(getResources().getColor(R.color.buttontextcolor));
                }
            }
        };
        collectModel.bindCallback(call, judgeStatusCallback);
    }

    private void collect(){
        collectModel = new CollectModel();
        call = collectModel.useroperation(getUser_id(), intent.getStringExtra("qid"));
        statusCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if(status.getCode().equals("1")){
                    if(collectstatus.equals("1")){
                        detail_collect_img.setImageResource(R.drawable.ic_collect_normal);
                        detail_collect.setText("未收藏");
                        detail_collect.setTextColor(getResources().getColor(R.color.textcolor));
                        collectstatus="0";
                    }else{
                        detail_collect_img.setImageResource(R.drawable.ic_collect_pressed);
                        detail_collect.setText("已收藏");
                        detail_collect.setTextColor(getResources().getColor(R.color.buttontextcolor));
                        collectstatus="1";
                    }
                }else{
                    Toast.makeText(QuestionContentActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        collectModel.bindCallback(call,statusCallback);
    }

    private void ThumbUp(){
        userModel = new UserModel();
        call = userModel.ThumbUp(intent.getStringExtra("qid"));
        statusCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if(status.getCode().equals("1")){
                    detail_thumbup_img.setImageResource(R.drawable.ic_praise_pressed);
                    detail_thumbup.setTextColor(getResources().getColor(R.color.buttontextcolor));
                    detail_point_number.setText(String.valueOf(Integer.parseInt(detail_point_number.getText().toString())+1));
                }
            }
        };
        userModel.bindCallback(call,statusCallback);
    }

    @Override
    public void onResume(){
        super.onResume();
        initData();
    }
}
